package com.gxa.admin.dao.impl;

import com.gxa.admin.dao.BannerDao;
import com.gxa.admin.pojo.dto.BannerDto;
import com.gxa.admin.pojo.vo.BannerVo;
import com.gxa.pojo.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository  //访问Dao层的注解
public class BannerDaoImpl implements BannerDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public BannerVo findBannerAll(BannerDto bannerDto) {
        Query query = new Query();
        long count = mongoTemplate.count(query,Banner.class);
        query.skip(bannerDto.getStartIndex());
        query.limit(bannerDto.getLimit());
        List<Banner> bannerList = mongoTemplate.find(query, Banner.class);
        BannerVo bannerVo = new BannerVo();
        bannerVo.setBannerList(bannerList);
        bannerVo.setCount(count);
        query.with(Sort.by(Sort.Direction.ASC,"ordered"));
        return bannerVo;

    }

    @Override
    public void insertBanner(Banner banner) {
        mongoTemplate.save(banner);

    }

    @Override
    public void deleteBannerById(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Banner.class);
    }


    @Override
    public void updateBanner(Banner banner) {
        mongoTemplate.save(banner);

    }
}
